﻿using ComLib.ComFun;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CopyNewFile
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //int age = IDCardCheck.GetAge("20170131");
            //输入
            Console.Write("请输入时分（yyyy-MM-dd HH:mm）：");
            string str = Console.ReadLine() + ":00";
            //if (str.Length != 14)
            //{
            //    Console.WriteLine("输入有误：{0}！！", str);
            //    Console.ReadKey();
            //    return;
            //}
            DateTime dt = DateTime.Now.AddMinutes(-5);
            if (!string.IsNullOrWhiteSpace(str))
            {
                if (!DateTime.TryParse(str, out dt))
                {
                    Console.WriteLine("输入时间格式有误：{0}！！", str);
                    Console.ReadKey();
                    return;
                }
            }
           

            string rootDir = AppDomain.CurrentDomain.BaseDirectory;
            string updateDir = Path.GetFullPath(Path.Combine(rootDir, "..", $"Update_{DateTime.Now.ToString("yyyyMMddHHmmss")}")); // 获取上一级路径
            if (!Directory.Exists(updateDir))
            {
                Directory.CreateDirectory(updateDir);
            }

            //遍历当前目录的全部目录和文件            
            string[] allFiles = Directory.GetFiles(rootDir, "*", SearchOption.AllDirectories);
            Console.WriteLine($"开始查询{rootDir}目录下的文件,共{allFiles.Length}个文件");
            int count = 0;
            int curProgress = 0;
            for (int i = 0; i < allFiles.Length; i++)
            {
                FileInfo fi = new FileInfo(allFiles[i]);
                if (fi.Name == "CopyNewFile.exe")
                    continue;
                //if (fi.Extension == ".pdb" || fi.Extension == ".ini"|| fi.Extension == ".xml" || fi.Extension == ".config")
                if (fi.Extension != ".exe" && fi.Extension != ".dll")
                    continue;
                int progress = (int)((((float)(i + 1)) / (float)allFiles.Length) * 100);
                if (progress != 0 && progress != curProgress && progress % 5 == 0)
                {
                    curProgress = progress;
                    Console.WriteLine($"已完成{progress}%");
                }

                if (fi.LastWriteTime > dt)
                {
                    string dest = Path.Combine(updateDir, allFiles[i].Remove(0, rootDir.Length));
                    string destDir = Path.GetDirectoryName(dest);
                    if (!Directory.Exists(destDir))
                    {
                        Directory.CreateDirectory(destDir);
                    }
                    string source = allFiles[i];
                    File.Copy(source, dest, true);
                    count++;
                }
            }
            Console.WriteLine($"导出完成,数量:{count}");
            Console.ReadKey();
        }
    }
}
